﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace HobSoft.SoccerBet2014.Domain.Common.Site
{
    using SiteContext;
    using Domain.Models;

    public partial class SboSessionManager : SessionManagerBase
    {
        public enum SboMarketType
        {
            Unkonw = 0,
            //全场让球
            FHDP = 1,
            FOE = 2,
            //全场大小球
            FOU = 3,
            FCS = 4,
            //全场标盘
            F1X2 = 5,
            FTG = 6,
            //半场让球
            HHDP = 7,
            //半场1X2
            H1X2 = 8,
            //半场大小球
            HOU = 9,
            HTFT = 10,
            ML = 11,
            HOE = 12,
            FGLG = 13,
            HCS = 14,
            DC = 15,
            LS = 16,
            HLS = 17
        }

        #region mapping
        Dictionary<SboMarketType, Odds.MarketType> MarketTypeMapping = new Dictionary<SboMarketType, Odds.MarketType>()
                        {
                            { SboMarketType.HHDP, Odds.MarketType.FHHDP},
                            { SboMarketType.FHDP, Odds.MarketType.GHDP},
                            { SboMarketType.H1X2, Odds.MarketType.FH1X2},
                            { SboMarketType.F1X2, Odds.MarketType.G1X2},
                            { SboMarketType.HOU, Odds.MarketType.FHOU},
                            { SboMarketType.FOU, Odds.MarketType.GOU}
                        };

        Dictionary<SboMarketType, OddsPrice.OddsPriceType[]> MarketPriceMapping = new Dictionary<SboMarketType, OddsPrice.OddsPriceType[]>()
        {
            { SboMarketType.F1X2, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Home, OddsPrice.OddsPriceType.Tie, OddsPrice.OddsPriceType.Away}},
            { SboMarketType.H1X2, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Home, OddsPrice.OddsPriceType.Tie, OddsPrice.OddsPriceType.Away}},
            { SboMarketType.FHDP, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Home, OddsPrice.OddsPriceType.Away}},
            { SboMarketType.HHDP, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Home, OddsPrice.OddsPriceType.Away}},
            { SboMarketType.FOU, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Over, OddsPrice.OddsPriceType.Under}},
            { SboMarketType.HOU, new OddsPrice.OddsPriceType[]{ OddsPrice.OddsPriceType.Over, OddsPrice.OddsPriceType.Under}}
        };

        Dictionary<OddsPrice.OddsPriceType, string> PriceTypeMappingAlias = new Dictionary<OddsPrice.OddsPriceType, string>
        {
            {OddsPrice.OddsPriceType.Home, "h:1"},
            {OddsPrice.OddsPriceType.Away, "a:2"},
            {OddsPrice.OddsPriceType.Tie, "x"},
            {OddsPrice.OddsPriceType.Over, "h"},
            {OddsPrice.OddsPriceType.Under, "a"}
        };
        #endregion

        #region class
        protected internal class SboEventResult
        {
            public string EventID { get; set; }
            public string ResultID { get; set; }
            public byte? HomeScore { get; set; }
            public byte? AwayScore { get; set; }
            public string RawJsonString { get; set; }
        }

        protected internal class SboOddsDisplayParamters
        {
            public SboOddsDisplayParamters()
            {
                Token = 1;
                Sport = 1;
                MarketPage = 1;
                PriceStyle = 1;
                Sorting = 1;
                MarketShowing = 2;
                OddsMode = 1;
                EventData = 2;
                DaylightType = 1;
            }
            public int Token { get; set; }
            public int Sport { get; set; }
            public int MarketPage { get; set; }
            public int PriceStyle { get; set; }
            public int Sorting { get; set; }
            public int MarketShowing { get; set; }
            public int OddsMode { get; set; }
            public int EventData { get; set; }
            public int DaylightType { get; set; }
            //'token', 'sport', 'market-page', 'price-style', 'sorting', 'market-showing', 'odds-mode', 'event-date','daylight-type'
            public override string ToString()
            {
                return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}", Token, Sport, MarketPage, PriceStyle, Sorting, MarketShowing, OddsMode, EventData, DaylightType);
            }
        }

        protected internal class SboSyncData
        {
            public int Version { get; set; }
            public DateTime LastRequestTime { get; set; }
            public int RequestTime { get; set; }
        }
        #endregion

        protected override string SessionID
        {
            get { return "ASP.NET_SessionId"; }
        }
        public Context SessionLoginName { get; protected set; }
        public string SignupTickKey { get; protected set; }
        public string SignupTickId { get; protected set; }
        protected string HtmlHomePage { get; set; }
        protected IList<string> SiteTokenKeys { get; set; }
        protected IList<string> SiteTokenValues { get; set; }
        protected IList<string> OdTokenKeys { get; set; }
        protected IList<string> OdTokenValues { get; set; }
        protected internal SboOddsDisplayParamters OddsDisplayParamters { get; set; }
        protected internal SboOddsDisplayParamters EarlyDisplayParamters { get; set; }
        protected internal SboSyncData EarlyMarketSync { get; set; }
        protected internal SboSyncData NoLiveSync { get; set; }
        protected internal SboSyncData LiveSync { get; set; }


        Regex regJsUnicode = new Regex(@"\\[uU]([0-9A-F]{4})");
        Regex regJsHex = new Regex(@"\\[xX]([\w]{2})");
        Regex regSiteToken = new Regex(@"(?:\$P\.setToken\('site'[^\[\];]+\[(?<k>[^\[\];]+)\][^\[\];]+\[(?<v>[^\[\];]+)\][^;]+;)", RegexOptions.Compiled);
        Regex regOdToken = new Regex(@"(?:\$P\.setToken\('od'[^\[\];]+\[(?<k>[^\[\];]+)\][^\[\];]+\[(?<v>[^\[\];]+)\][^;]+;)", RegexOptions.Compiled);
        Regex regLoginName = new Regex(@"(?:loginname=(?<v>[\w]+)&?)", RegexOptions.Compiled);
        Regex regLiveData = new Regex(@"(?:\.onUpdate\((?<v>\d?)\s*,\s*\[(?<j>.+)\]\s*\);\s*$)", RegexOptions.Compiled);
        Regex regLiveIdleData = new Regex(@"(?:\.onIdle\((?<v>\d?)\s*,\s*\[(?<j>.+)\]\s*\);\s*$)", RegexOptions.Compiled);
        Regex regOddsDisplayParam = new Regex(@"(?:var oddsDisplayParam = TiLib.Parameter.createParameter\((?<n>\[[^\[\]]+\])\s*,\s*(?<v>\[[^\[\]]+\])\);)", RegexOptions.Compiled);
        Regex regBettingResultOrder = new Regex(@"(?<=onUpdateBetReceipt\(')(?<id>\d+)',(?<j>\[.+\])(?=\);\$M)", RegexOptions.Compiled);
        Regex regBettingResultCode = new Regex(@"showOrderResult\((?<c>\d+),", RegexOptions.Compiled);
        Regex regBettingMiniOrder = new Regex(@"(?<=onUpdate\()(?<j>\[.+\])(?=,\d*\);)", RegexOptions.Compiled);
        Regex regMiniOrderHandicap = new Regex(@"(?<t>[^\s]+)\s*\[(?<h>\-?\d+\.?\d*)(@.+)?\]", RegexOptions.Compiled);
        Regex regHisOrderHandicap = new Regex(@"(?<t>[^@\s]+)@?[^@\s]*\s*(?<h>\d(\.\d+)?)?", RegexOptions.Compiled);
    }
}
